<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="queryParams.username" :placeholder="$t('table.loginLog.username')" class="filter-item search-item" />
      <el-button class="filter-item" type="primary" plain @click="search">
        {{ $t('table.search') }}
      </el-button>
      <el-button class="filter-item" type="warning" plain @click="reset">
        {{ $t('table.reset') }}
      </el-button>
    </div>
    <el-table
      ref="table"
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      style="width: 100%;"
    >
      <el-table-column :label="$t('table.loginLog.username')" prop="username" :show-overflow-tooltip="true" align="center" min-width="100px">
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.loginLog.ip')" prop="ip" :show-overflow-tooltip="true" align="center" min-width="120px">
        <template slot-scope="scope">
          <span>{{ scope.row.ip }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.loginLog.location')" prop="location" :show-overflow-tooltip="true" align="center" min-width="120px">
        <template slot-scope="scope">
          <span>{{ scope.row.loginAddress }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.loginLog.loginTime')" prop="loginTime" :show-overflow-tooltip="true" align="center" min-width="150px" sortable>
        <template slot-scope="scope">
          <span>{{ scope.row.loginTime }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.operation')" align="center" min-width="90px" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <i v-has-permission="['user:kickout']" class="el-icon-delete table-operation" style="color: #f50;" @click="singleDelete(row)" />
          <el-link v-has-no-permission="['user:kickout']" class="no-perm">
            {{ $t('tips.noPermission') }}
          </el-link>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import db from '@/utils/localstorage'
export default {
  name: 'Online',
  components: {},
  data() {
    return {
      tableKey: 0,
      loading: false,
      list: null,
      queryParams: {}
    }
  },
  computed: {
    user() {
      return this.$store.state.account.user
    }
  },
  mounted() {
    this.fetch()
  },
  methods: {
    singleDelete(row) {
      this.$confirm('当您点击确定按钮后，该用户的登录将会马上失效, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$delete(`kickout/${row.id}`).then(() => {
          if (this.user.id === row.id) {
            db.clear()
            location.reload()
          } else {
            this.search()
            this.$message({
              type: 'success',
              message: '踢出成功!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    reset() {
      this.queryParams = {}
      this.$refs.table.clearSort()
      this.$refs.table.clearFilter()
      this.search()
    },
    search() {
      this.fetch({
        ...this.queryParams
      })
    },
    fetch(params = {}) {
      this.$get(`online`, { ...params }).then((r) => {
        this.list = r.data.data
      })
    }
  }
}
</script>

<style scoped>

</style>
